

function getFieldTemplate(field, span = 8) {

    // const allTypes = ['input', 'select', 'date', 'datetime', 'textarea', 'radio', 'checkbox', 'switch', 'file', 'api-select', 'dict-select'];

    // 输入框
    if (field.fieldType === 'input') {
        return `<el-col :span="${span}">
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
                :rules="[{ required: true, message: '请输入${field.fieldLabel}', trigger: 'blur' }]"
            >
                <el-input
                    v-model="form.${field.fieldName}"
                    style="width: 100%"
                    placeholder="请输入"
                />
            </el-form-item>
        </el-col>
        `
    }

    // 选择框
    if (field.fieldType === 'select') {
        return `<el-col :span="${span}">
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
                :rules="[{ required: true, message: '请选择${field.fieldLabel}', trigger: 'blur' }]"
            >
                <el-select v-model="form.${field.fieldName}" style="width: 100%" placeholder="请选择${field.fieldLabel}">
                    <el-option label="选项一" value="0"></el-option>
                    <el-option label="选项二" value="1"></el-option>
                </el-select>
            </el-form-item>
        </el-col>
        `
    }

    // 日期选择框
    if (field.fieldType === 'date') {
        return `<el-col :span="${span}">
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
                :rules="[{ required: true, message: '请选择${field.fieldLabel}', trigger: 'blur' }]"
            >
                <el-date-picker
                    v-model="form.${field.fieldName}"
                    type="date"
                    valueFormat="YYYY-MM-DD"
                    placeholder="${field.fieldLabel}"
                    style="width: 100%"
                />
            </el-form-item>
        </el-col>
        `
    }

    // 日期时间选择框
    if (field.fieldType === 'datetime') {
        return `<el-col :span="${span}">
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
                :rules="[{ required: true, message: '请选择${field.fieldLabel}', trigger: 'blur' }]"
            >
                <el-date-picker
                    v-model="form.${field.fieldName}"
                    type="datetime"
                    valueFormat="YYYY-MM-DD HH:mm:ss"
                    placeholder="${field.fieldLabel}"
                    style="width: 100%"
                />
            </el-form-item>
        </el-col>
        `
    }

    // 多行文本框
    if (field.fieldType === 'textarea') {
        return `<el-col :span="24">
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
            >
                <el-input
                    v-model="form.${field.fieldName}"
                    maxlength="500"
                    type="textarea"
                    placeholder="请输入"
                />
            </el-form-item>
        </el-col>
        `
    }

    // 附件
    if (field.fieldType === 'file') {
        return `<el-col :span="24">
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
            >
                <OproUpload
                    v-model="form.${field.fieldName}"
                    listType="text"
                    @change="handleFileChange"
                ></OproUpload>
            </el-form-item>
        </el-col>
        `
    }


}

function getSearchFieldTemplate(field) {
    // const allTypes = ['input', 'select', 'date', 'textarea', 'radio', 'checkbox', 'switch', 'file'];

    const searchType = ['input', 'select', 'date'];
    const isSearchType = searchType.includes(field.fieldType);

    if (!isSearchType) {
        return `
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
            >
                <el-input
                    v-model="queryParams.${field.fieldName}"
                    style="width:160px"
                    clearable
                    placeholder="请输入${field.fieldLabel}"
                    @input="handleQuery"
                />
            </el-form-item>
        `
    }

    if (field.fieldType === 'input') {
        return `
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
            >
                <el-input
                    v-model="queryParams.${field.fieldName}"
                    style="width:160px"
                    clearable
                    placeholder="请输入${field.fieldLabel}"
                    @input="handleQuery"
                />
            </el-form-item>
        `
    }

    if (field.fieldType === 'select') {
        return `
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
            >
                <el-select
                    v-model="queryParams.${field.fieldName}"
                    style="width: 160px"
                    clearable
                    @change="handleQuery"
                >
                    <el-option label="选项一" value="0"></el-option>
                    <el-option label="选项二" value="1"></el-option>
                    <el-option label="选项三" value="2"></el-option>
                </el-select>
            </el-form-item>
        `
    }

    if (field.fieldType === 'date') {
        return `
            <el-form-item
                label="${field.fieldLabel}"
                prop="${field.fieldName}"
            >
                <el-date-picker
                    v-model="queryParams.${field.fieldName}"
                    type="daterange"
                    rangeSeparator="至"
                    style="width: 443px"
                    startPlaceholder="开始日期"
                    endPlaceholder="结束日期"
                    valueFormat="YYYY-MM-DD"
                    @change="handleQuery"
                />
            </el-form-item>
        `
    }
}

function hasFileField(config) {
    return config.tableFields.some(field => field.fieldType === 'file');
}

function hasDictFiled(config) {

}

module.exports = {
    getFieldTemplate: getFieldTemplate,
    getSearchFieldTemplate: getSearchFieldTemplate,
    hasFileField: hasFileField,
    hasDictFiled: hasDictFiled
};                   

